package com.zh.work.controller.platform.trans;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.common.util.DateSetUtil;
import com.zh.work.entity.trans.QuickOrderEntity;
import com.zh.work.service.platform.trans.QuickOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 快捷订单控制器
 * @Author: Li.HuiNeng
 * @Create in: 2020-01-07 14:03
 * @Version: 1.0.1
 */
@Api(value = "快捷订单控制器", produces = "application/json", tags = {"快捷订单控制器"})
@RestController
@RequestMapping("/quickOrder")
public class QuickOrderController {

    @Autowired
    private QuickOrderService service;


    @ApiOperation(value = "分页查询快捷订单数据集", tags = {"getQuickOrderPages"}, notes = "分页查询快捷订单数据集")
    @GetMapping("/getQuickOrderPages")
    public JSONResult<List<QuickOrderEntity>> getQuickOrderPages(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "快捷订单实体类", name = "QuickOrderEntity") QuickOrderEntity quickOrderEntity) {
        JSONResult<List<QuickOrderEntity>> jsonResult = new JSONResult<>();
        try {
            Page<QuickOrderEntity> page = new Page<>(pageNo, pageSize);
            IPage<QuickOrderEntity> result = service.getQuickOrderPages(page, quickOrderEntity);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询快捷订单统计数据", tags = {"getQuickOrderStatistic"}, notes = "查询快捷订单统计数据")
    @GetMapping("/getQuickOrderStatistic")
    public JSONResult<List<Map<String, String>>> getQuickOrderStatistic(@ApiParam(value = "快捷订单实体类", name = "QuickOrderEntity") QuickOrderEntity QuickOrderEntity) {
        JSONResult<List<Map<String, String>>> jsonResult = new JSONResult<>();
        List<Map<String, String>> list = null;
        if (Objects.isNull(QuickOrderEntity.getStartDateTime())) {
            QuickOrderEntity.setStartDateTime(DateSetUtil.getDayFirstSec(new Date()));
        }
        if (Objects.isNull(QuickOrderEntity.getEndDateTime())) {
            QuickOrderEntity.setEndDateTime(DateSetUtil.getDayLastSec(new Date()));
        }
        try {
            list = this.service.getOnlineOrderStatistic(QuickOrderEntity);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
        }
        if (list != null) {
            if (list.size() > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setData(list);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("统计数量为0");
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("统计数量为空");
        }
        return jsonResult;
    }
}
